package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.ui.MenuItem;
import com.sound.ampache.objects.Song;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;


class CurrentPlaylistScreen extends MainScreen {
	
	private Song[] songs;
	private SongsListField songsList;
	private int fontSize;
	private int rowHeight;
	public String screenTitle = "Current Playlist";
	
	Font mainFont = Font.getDefault().derive(Font.PLAIN, 24);
     
     private MenuItem delSongMenu = new MenuItem("Delete Song from Playlist",100,10) {
         public void run() {
             delSong();
         } 
      };
      
      private MenuItem clearMenu = new MenuItem("Clear Playlist",100,10) {
          public void run() {
              clearPlaylist();
          } 
       };
       
       private MenuItem playMenu = new MenuItem("Play Song",100,10) {
           public void run() {
               playSong();
           } 
        };
    
	CurrentPlaylistScreen()
	{		
		setFont(mainFont);
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        songsList = new SongsListField();
        setRowHeight();
        songsList.setEmptyString("No Songs", DrawStyle.HCENTER);
        this.add(songsList); 
        Bitmap icon = Bitmap.getBitmapResource("img/download.png"); 
        new Dialog("Downloading...",null,null,0,icon); 	
	}
	
	
	public void setRowHeight() {
		//Get user selected font size and set screen font sizes
		fontSize = Integer.parseInt(DataStore.getFontSize());
		rowHeight = fontSize * 3 + 5;
        songsList.setRowHeight(rowHeight);
        songsList.setFont(Font.getDefault().derive(Font.PLAIN, fontSize));
	}
	
	
	public void getList() {
		songs = new Song[ampacheberry.playlistSong.songPlaylist.size()];
		ampacheberry.playlistSong.songPlaylist.copyInto(songs);
		songsList.set(songs);
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
        menu.add(delSongMenu);
        menu.add(clearMenu);
        menu.add(playMenu);
        menu.add(MenuItem.separator(clearMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }

    
    private void clearPlaylist() {
    	int selected = songsList.getSelectedIndex();
    	if (selected >= 0) {
    		if(ampacheberry.playlistSong.clearPlaylist()){
    			getList();
    		}
    	}
    }
    
    
    private void delSong() {
    	int selected = songsList.getSelectedIndex();
    	if (selected >= 0) {
    		if (ampacheberry.playlistSong.delSong(selected)) {
    			ampacheberry.playlistSong.writePlaylist();
    			getList();
    			this.setHighligt(Math.min(selected, ampacheberry.playlistSong.songPlaylist.size()));
    		}
    	}
    }
    
    
    private void playSong() {
    	int selected = songsList.getSelectedIndex();
    	if (selected >= 0) {
	    	ampacheberry.playerScreen.stop();
	    	ampacheberry.playlistSong.playSongNum(selected);
	    	ampacheberry.playerScreen.play();
	    	try {
	    		UiApplication.getUiApplication().popScreen(ampacheberry.playlistScreen);
	    	} catch(IllegalArgumentException e) {
	    		/*screen is not on the stack - do nothing*/
	    	}
	    	if (ampacheberry.playerScreen.isDisplayed()) {
	    		UiApplication.getUiApplication().popScreen(ampacheberry.playerScreen);
	    	}
	    	UiApplication.getUiApplication().pushScreen(ampacheberry.playerScreen);
    	}
    }
    
    
    public void setHighligt(int rowIndex){
    	songsList.setSelectedIndex(rowIndex);
    }
    
    
    private class SongsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/song.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                    
            int offsetY = (this.getRowHeight() - icon.getHeight())/2;
            graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                    icon.getHeight(),icon,0,0);
            graphics.drawText("Song: " + songs[index].name, 
                icon.getWidth() + 2, y + 1, DrawStyle.ELLIPSIS,
                width - icon.getWidth() + 2);
            graphics.drawText("Artist: " + songs[index].artist, 
                    icon.getWidth() + 2, y + fontSize + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawText("Album: " + songs[index].album, 
                    icon.getWidth() + 2, y + (2 * fontSize) + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawLine(0, y + rowHeight - 1, listField.getWidth(),
            		y + rowHeight - 1);
        }
    };
    
} 
